package com.medical.medical.controller.gyncontroller;

import com.medical.medical.entity.Patient;
import com.medical.medical.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @authorDesc 你相信光吗？
 * @author GUOYN
 * @date 2022-09-14 08:42:52
 * @version 1.0.0
 * @description 患者信息请求层
 */
@Slf4j
@Controller
public class GYNPatientController {

    @Resource
    private PatientService patientService;
    /**
     *每页展示的信息条数
     */
    private static final int PAGE_SIZE = 5;
    /**
      * @description 跳转到主页面
      * @author GUOYN
      * @date 2022-09-14 09:08:07  * @param
      * @return {@link String}
      */
    @RequestMapping("toadminhome")
    public String toAdminHome(){
        return "/admin/adminhome";
    }
    /**
      * @description 根据条件查询患者信息
      * @author GUOYN
      * @date 2022-09-14 09:15:23  * @param
      * @return {@link String}
      */
    @RequestMapping("patientlist")
    public String getPatientList(@RequestParam(required = false) String phone,
                                 @RequestParam(required = false) String name,
                                 @RequestParam(required = false) String IDCard,
                                 @RequestParam(required = false) String currentPage,
                                 HttpSession session,
                                 Model model){
        long phone1 = 0;
        if (phone != null && !phone.equals("") ){
            try {
                phone1 = Long.valueOf(phone.replace(" ",""));
            }catch (NumberFormatException e){
                e.printStackTrace();
                log.info("获得填写的手机号时发生异常！");
            }
        }

        log.info("-------开始查询-------");
        //获得当前页码
        int current = 0;
        if (currentPage != null && !currentPage.equals("")){
            try {
                current = Integer.valueOf(currentPage);
            }catch (NumberFormatException e){
                e.printStackTrace();
                log.info("获得当前页码时发生异常！");
            }
        }
        //获得条件下表中数据总条数
        int totalCount = patientService.getPatientCount(phone1,name,IDCard);
        if (totalCount == 0){
            model.addAttribute("totalCountFlag",1);
            model.addAttribute("result","在当前条件下没有用户/患者信息！");
            return "/admin/patientlist";
        }
        //计算总页数
        int totalPageCount = 0;
        if (totalCount%PAGE_SIZE==0){
            totalPageCount = totalCount/PAGE_SIZE;
        }else {
            totalPageCount = totalCount/PAGE_SIZE + 1;
        }
        //控制首页和尾页
        if (current < 1){
            current = 1;
        }else if (current > totalPageCount){
            current = totalPageCount;
        }
        //计算当前页信息表中的起始下标
        int start = (current - 1) * PAGE_SIZE;
        //获得当前页面信息
        List<Patient> patientList = patientService.getPatientList(phone1,name,IDCard,start,PAGE_SIZE);
        for (Patient patient:patientList) {
            patient.setIDCard(patient.getIDCard().substring(0,4)+"**********"+patient.getIDCard().substring(14));
        }
        //将取得数据返回到页面上
        if(patientList != null && patientList.isEmpty()){
            model.addAttribute("patientFlag",1);
            log.info("在当前条件下没有用户/患者信息！");
            model.addAttribute("result","在当前条件下没有用户/患者信息！");
        }else {
            model.addAttribute("patientList",patientList);
            log.info("在当前条件下获得用户/患者信息成功！！");
        }
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("phone",phone);
        model.addAttribute("name",name);
        model.addAttribute("IDCard",IDCard);
        model.addAttribute("current",current);
        model.addAttribute("totalPageCount",totalPageCount);
        return "/admin/patientlist";
    }
    /**
      * @description 根据id获得用户/患者详情
      * @author GUOYN
      * @date 2022-09-14 10:01:04  * @param id
     * @param model
      * @return {@link String}
      */
    @RequestMapping("patientview")
    public String getPatientView(int id,Model model,HttpSession session){
        Patient patient = patientService.getPatientView(id);
        List<Date> recordTimeList = new ArrayList<>();
        if (patient != null){
            //获得当前用户的就诊状态
            int regState = patientService.getRegStateByIDCard(id);
            log.info("{}",regState);
            if(regState != 0){
                //获得当前用户的就诊时间
                recordTimeList = patientService.getRecordTimeByIDCard(id);
                log.info("{}",recordTimeList);
            }else {
                log.info("当前用户未就诊");
            }
            log.info("获得当前用户/患者详细信息成功！");
            model.addAttribute("patient",patient);
            model.addAttribute("regState",regState);
            model.addAttribute("recordTimeList",recordTimeList);
            return "/admin/patientview";
        }else {
            session.setAttribute("result","获得当前用户/患者详细信息失败！请重新获取！");
            log.info("获得当前用户/患者详细信息失败！");
            return "redirect:/patientlist";
        }
    }
    /**
      * @description 根据id删除用户/患者
      * @author GUOYN
      * @date 2022-09-14 10:03:16  * @param id
     * @param session
      * @return {@link String}
      */
    @RequestMapping("patientdel")
    public String delPatientById(int id,HttpSession session,Model model){
        boolean flag = patientService.delPatientById(id);
        if (flag){
            log.info("根据id删除用户/患者成功！");
            model.addAttribute("result","删除成功！");
        }else {
            log.info("根据id删除用户/患者失败！");
            session.setAttribute("result","删除失败！");
        }
        return "redirect:/patientlist";
    }


}
